package com.A01.sys.service.impl;

import com.A01.sys.dao.RoleDao;
import com.A01.sys.entity.RoleEntity;
import com.A01.sys.entity.RoleMenuEntity;
import com.A01.sys.service.RoleMenuService;
import com.A01.sys.service.RoleService;
import com.A01.utils.ContextHolderUtils;
import com.A01.utils.PageUtils;
import com.A01.utils.Query;
import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.*;


@Service("roleService")
public class RoleServiceImpl extends ServiceImpl<RoleDao, RoleEntity> implements RoleService {

//    @Autowired
//    private DeptService deptService;
    @Autowired
    private RoleMenuService roleMenuService;
//    @Autowired
//    private RoleAppMenuService roleAppMenuService;
//    @Autowired
//    private RoleDeptService roleDeptService;
    @Autowired
    private RoleDao roleDao;

    /**
     *利用分页工具和条件构造器实现通过角色姓名进行分页查询功能
     */
    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        String roleName = (String) params.get("roleName");
        Page<RoleEntity> page = this.selectPage(
                new Query<RoleEntity>(params).getPage(),
                new EntityWrapper<RoleEntity>()
                        .like(StringUtils.isNotBlank(roleName), "role_name", roleName)
        );

        return new PageUtils(page);
    }

    /**
     *新增角色
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public void save(RoleEntity role) {
        role.setCreateTime(new Date());
        this.insert(role);
        saveRoleMenuRelation(role);
//        saveRoleDeptRelation(role);
    }

    /**
     *修改用户
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public void update(RoleEntity role) {
        this.updateById(role);
        saveRoleMenuRelation(role);
//        saveRoleDeptRelation(role);
    }

    /**
     *通过用户id获得角色
     */
    @Override
    public List<RoleEntity> getRoleByUserId(Long userId) {
        return roleDao.getRoleByUserId(userId);
    }

    /**
     *通过用户id查询所有的角色
     */
    @Override
    public List<RoleEntity> selectAll(Map<String, Object> params) {
        Long userId = ContextHolderUtils.getUser().getUserId();
        Long role_id;
        //系统管理员，拥有最高权限
        if (userId == 1) {
            role_id = 0l;
        } else {
            RoleEntity role = selectByUserId(userId);
            role_id = role.getRoleId();
        }
        return roleDao.selectList(new EntityWrapper<RoleEntity>());
    }

    /**
     *根据用户id进行批量删除
     */
    @Override
    @Transactional
    public void deleteBatchIds(Long[] roleIds) {
        this.deleteBatchIds(Arrays.asList(roleIds));
        roleMenuService.deleteBatch(Arrays.asList(roleIds));
    }

    /**
     *通过角色名得到角色id
     */
    @Override
    public Long getRoleIdByName(String roleName) {
        return baseMapper.getRoleIdByName(roleName);
    }

    /**
     *通过用户id查询对应的角色
     */
    @Override
    public RoleEntity selectByUserId(Long userId) {
        return baseMapper.selectByUserId(userId);
    }

    /**
     * 选解除之前角色和菜单关系，再绑定关系
     */
    private void saveRoleMenuRelation(RoleEntity role) {
        List<Long> roleIds = new ArrayList<>(1);
        roleIds.add(role.getRoleId());
        roleMenuService.deleteBatch(roleIds);

        List<Long> menuIdList = role.getMenuIdList();
        if (menuIdList.size() == 0) {
            return;
        }

        List<RoleMenuEntity> roleMenuEntities = new ArrayList<>(menuIdList.size());
        for (int i = 0; i < menuIdList.size(); i++) {
            RoleMenuEntity roleMenuEntity = new RoleMenuEntity();
            roleMenuEntity.setRoleId(role.getRoleId());
            roleMenuEntity.setMenuId(menuIdList.get(i));
            roleMenuEntities.add(roleMenuEntity);
        }
        roleMenuService.insertBatch(roleMenuEntities);
    }
}
